package com.example.qt.ptplatform.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.qt.ptplatform.entity.PointRecord;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface PointRecordMapper extends BaseMapper<PointRecord> {
    // 自定义批量插入方法
    @Insert("<script>" +
            "INSERT INTO point_records (user_id, points, torrent_id) VALUES " +
            "<foreach collection='records' item='item' separator=','>" +
            "(#{item.userId}, #{item.points}, #{item.torrentId})" +
            "</foreach>" +
            "</script>")
    int batchInsert(@Param("records") List<PointRecord> records);

    //获取指定用户当天日期的积分记录
    @Select("SELECT * FROM point_records " +
            "WHERE user_id = #{userId} " +
            "AND point_type = 'keepSeed' " +
            "AND DATE(create_time) = CURDATE() " +
            "ORDER BY create_time DESC")
    List<PointRecord> getKeepSeedPointsByUser(@Param("userId") Integer userId);

    // 查询指定用户在指定时间范围内的保种积分变化
    @Select("SELECT SUM(points) FROM point_records " +
            "WHERE user_id = #{userId} " +
            "AND point_type = 'keepSeed' " +
            "AND create_time BETWEEN #{startTime} AND #{endTime}")
    Double getSeedPointsChange(@Param("userId") Integer userId,
                               @Param("startTime") LocalDateTime startTime,
                               @Param("endTime") LocalDateTime endTime);
}
